package com.wenli.lt.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wenli.lt.entity.User;
import com.wenli.lt.service.IUserService;
import com.wenli.lt.util.CommonResponse;
import com.wenli.lt.util.LoginCommonResponse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lt
 * @since 2021-03-21
 */
@RestController
@Api(tags="用户接口")
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @GetMapping("/getUserInfo")
    @ApiOperation("根据学号查找个人基本信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
    })
    public CommonResponse getUserInfo(String studentNumber  ){
        return new CommonResponse(iUserService.getUserInfo(studentNumber));
    }

    @GetMapping("/getUserInfoByStudentNumber")
    @ApiOperation("点击列表详情 查看人员详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
    })
    public CommonResponse getUserInfoByStudentNumber(String studentNumber  ){
        return new CommonResponse(iUserService.getUserInfoByStudentNumber(studentNumber));
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
            @ApiImplicitParam( name = "oldPassword",value = "旧账号密码",required = true),
            @ApiImplicitParam( name = "newPassword",value = "新账号密码",required = true),
    })
    public LoginCommonResponse UpdatePassword(String studentNumber,String oldPassword,String  newPassword ){
        Boolean aBoolean = iUserService.UpdatePassword(studentNumber, oldPassword, newPassword);
        if (aBoolean){
            return new LoginCommonResponse(null,2);
        }else {
            return new LoginCommonResponse(null,3);
        }
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation("修改基本信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
            @ApiImplicitParam( name = "nickname",value = "昵称"),
            @ApiImplicitParam( name = "phoneNumber",value = "手机号"),
            @ApiImplicitParam( name = "avater",value = "头像地址"),
    })
    public LoginCommonResponse updateUserInfo(String studentNumber,String nickname,String  phoneNumber,String avater ){
        Boolean aBoolean = iUserService.updateUserInfo(studentNumber, nickname, phoneNumber,avater);
        if (aBoolean){
            return new LoginCommonResponse(null,2);
        }else {
            return new LoginCommonResponse(null,3);
        }
    }



    @PostMapping("/deleteUser")
    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
    })
    public LoginCommonResponse deleteUser(String studentNumber ){
        Boolean aBoolean = iUserService.deleteUser(studentNumber);
        if (aBoolean){
            return new LoginCommonResponse(null,2);
        }else {
            return new LoginCommonResponse(null,3);
        }
    }

    @PostMapping("/setUserLoginRelease")
    @ApiOperation("设置用户是否登录 是否发布")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
            @ApiImplicitParam( name = "isLogin",value = "是否登录"),
            @ApiImplicitParam( name = "isRelease",value = "是否发布"),
    })
    public CommonResponse setUserLoginRelease(String studentNumber,Integer  isLogin,Integer isRelease){
        Boolean aBoolean = iUserService.setUserLoginRelease(studentNumber,isLogin,isRelease);
        if (aBoolean){
            return new CommonResponse("修改成功!");

        }else {
            return new CommonResponse(CommonResponse.ERROR_CODE,"数据出错,请联系管理员");
        }
    }

    @GetMapping("/login")
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
            @ApiImplicitParam( name = "password",value = "账号密码",required = true),
            @ApiImplicitParam( name = "status",value = "登录系统(0后台管理系统 1商城  )",required = true),
    })
        public LoginCommonResponse login(String studentNumber, String password, Integer status){
        User login = iUserService.login(studentNumber, password, status);
        if (ObjectUtils.isEmpty(login)){
            return new LoginCommonResponse(null,1);
        }else {
            return new LoginCommonResponse(login,0);
        }
    }

    @GetMapping("/getTree")
    @ApiOperation("获取学校组织树")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "stauts",value = "传0 查询年级  传1查询学院树 不传查询全部"),
    })
    public CommonResponse getTree(Integer stauts){
        return new CommonResponse(iUserService.getTree(stauts));
    }

    @GetMapping("/getGoodLy")
    @ApiOperation("   商品留言 买家留言")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "studentNumber",value = "学生账号",required = true),
            @ApiImplicitParam( name = "type",value = "1 是留言 2 买家信息",required = true),
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "条数", required = true),
    })
    public CommonResponse getGoodLy(String studentNumber,Integer type, Long  page, Long size){
        return new CommonResponse(iUserService.getGoodLy(studentNumber,type,page,size));
    }

    @GetMapping("/getPeopleList")
    @ApiOperation("点击树形结构图  显示人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "classId",value = "班级ID"),
            @ApiImplicitParam( name = "collegeId",value = "学院ID"),
            @ApiImplicitParam( name = "gradeId",value = "年级ID"),
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "条数", required = true),
    })
    public CommonResponse getPeopleList(String classId,String collegeId,String gradeId, Long  page, Long size){
        return new CommonResponse(iUserService.getPeopleList(classId,collegeId,gradeId,page,size));
    }
    {

    }

    @ApiOperation(value = "上传多个图片")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "goodsId",value = "根据goodsId得有无插入图片库"),
    })
    @PostMapping("/manyFile")
    public CommonResponse uploadFile(@RequestBody @ApiParam(name="files",value = "多个图片文件",required = true) MultipartFile[] files,String goodsId) throws IOException {
        return new CommonResponse<>(iUserService.updateHead(files,goodsId));
    }


    @PostMapping("/addUser")
    @ApiOperation( value = "新增用户")
    public CommonResponse addUser(@RequestBody @ApiParam(name = "user",value = "用户(只传用户基本信息  学院年级班级传入班级ID)",required = true) User user){
        if (iUserService.addUser(user)){
            return new CommonResponse("新增成功");

        }else {
            return new CommonResponse(CommonResponse.ERROR_CODE,"数据出错,请联系管理员");
        }
    }







}
